const list = [
  { id: 1, name: '部门 A', parentId: 0 },
  { id: 2, name: '部门 B', parentId: 0 },
  { id: 3, name: '部门 C', parentId: 1 },
  { id: 4, name: '部门 D', parentId: 1 },
  { id: 5, name: '部门 E', parentId: 2 },
  { id: 6, name: '部门 F', parentId: 3 },
  { id: 7, name: '部门 G', parentId: 2 },
  { id: 8, name: '部门 H', parentId: 4 }]
function convert(list) {
  const res = []
  const map = list.reduce((init, element) => (init[element.id] = element, init), {})
  for (const item of list) {
    // 如果是最外层（祖级）的元素，直接放到数组中
    if (item.parentId === 0) {
      res.push(item)
      continue
    }
    if (item.parentId in map) {
      const parent = map[item.parentId]
      parent.children = parent.children || []
      parent.children.push(item)
    }
  }
  return res
}
// console.log(convert(list))

const data = [
  {
    id: '1',
    name: 'test1',
    children: [
      {
        id: '11',
        name: 'test11',
        children: [
          {
            id: '111',
            name: 'test111',
          },
          {
            id: '112',
            name: 'test112',
          },
        ],
      },
      {
        id: '12',
        name: 'test12',
        children: [
          {
            id: '121',
            name: 'test121',
          },
          {
            id: '122',
            name: 'test122',
          },
        ],
      },
    ],
  },
]
const find = (data, value) => {
  const result = []
  let findArr = data
  let skey = ''
  for (let i = 0, l = value.length; i < l; i++) {
    skey += value[i]
    const item = findArr.find((item) => item.id == skey)
    if (!item) {
      return []
    }
    result.push(item.id)
    if (item.children) {
      findArr = item.children
    } else {
      if (i < l - 1) {
        return []
      }
      return result
    }
  }
}
// console.log(find(data, '132'))
console.log(['1', '2', '3'].map((value) => parseInt(value)))